namespace ETestIT.Core.Services
{
    using System;
    using System.Collections.Generic;
    using DataInterfaces;
    using SharpArch.Core.PersistenceSupport;

    public class CandidateResumeServiceImpl : ICandidateResumeService
    {
        private readonly ICandidateResumeRepository candidateResumeRepository;

        public CandidateResumeServiceImpl(
            ICandidateResumeRepository candidateResumeRepository)
        {
            this.candidateResumeRepository = candidateResumeRepository;
        }

        #region ICandidateResumeService Members

        public void Create(CandidateResume candidateResume)
        {
            candidateResume.TheResume.Version ++;
            candidateResumeRepository.SaveOrUpdate(candidateResume);
        }

        public void Update(int previosResumeId, CandidateResume candidateResume)
        {
            var previosCandidateResume = candidateResumeRepository.GetByResumeId(previosResumeId);

            var updatedCandResume = new CandidateResume{
                   TheCandidate = candidateResume.TheCandidate,
                   TheResume = candidateResume.TheResume,
                   TheJobTitle = candidateResume.TheJobTitle
               };

            updatedCandResume.TheResume.Version = previosCandidateResume.TheResume.Version + 1;
            
            candidateResumeRepository.SaveOrUpdate(updatedCandResume);
        }

        public void Delete(int resumeId)
        {
            CandidateResume candidateResume = candidateResumeRepository.Get(resumeId);
            candidateResumeRepository.Delete(candidateResume);
        }

        public IList<CandidateResume> GetLatestVersionsResume(int candidateId)
        {
            return candidateResumeRepository.GetLatestVersions(candidateId);
        }

        public IList<Resume> GetHistoryByCanditadeResumeId(int candidateResumeId)
        {
            return candidateResumeRepository.GetResumesHistoryBy(candidateResumeId);
        }

        public CandidateResume Get(int id)
        {
            return candidateResumeRepository.Get(id);
        }

        public IDbContext DbContext
        {
            get { return candidateResumeRepository.DbContext; }
        }

        #endregion
    }
}